#pragma once

#include "Cond.hpp"
//#include"Mutex.hpp"
#include <queue>
#include <vector>
#include "Sem.hpp"
using std::cout;
using std::endl;

namespace zgw
{
    int gcapa = 4;
    template <class T>
    class Ringqueue
    {
    public:
        Ringqueue(int _capa = gcapa) : _sempro(_capa), _semcon(0), _capacity(_capa), _rqueue(_capa), _curpro(0), _curcon(0)
        {}

        void Set(T *data)
        {
            _sempro.P();
            {
                _mutexpro.Lock();
                _rqueue[_curpro++] = *data;
                _curpro %= _capacity;

                _mutexpro.Unlock();
            }
            _semcon.V();
        }
        void Get(T *data)
        {
            _semcon.P();
            {
                _mutexcon.Lock();
                *data = _rqueue[_curcon++];
                _curcon %=_capacity;
                _mutexcon.Unlock();
            }
            _sempro.V();
        }
        ~Ringqueue()
        {
        }

    private:
        int _curpro;
        int _curcon;
        std::vector<T> _rqueue;
        int _capacity;
        Sem _sempro;
        Sem _semcon;
        Mymutex _mutexcon;
        Mymutex _mutexpro;
    };
}

zgw::Ringqueue<int> rq;